const api = require('../APIConfig/apiConfig.js');
var app = getApp();
const checkLogin = function (params) {
  wx.showLoading({
    title: '正在登录...',
  })
  wx.login({
    success: function (loginRes) {
      if (loginRes.code) {
        wx.getUserInfo({
          withCredentials: true, // 非必填, 默认为true
          success: function (infoRes) {
            // console.log(infoRes, '>>>')
            // 请求服务端的登录接口
            wx.setStorageSync('photo', infoRes.userInfo.avatarUrl)
            wx.request({
              url: api.loginUrl,
              // header: {
              //   'content-type': 'application/x-www-form-urlencoded',
              //   'Accept': 'application/json'
              // },
              method: 'get',
              data: {
                code: loginRes.code,
                avatarUrl: infoRes.userInfo.avatarUrl,
                city: infoRes.userInfo.city,
                country: infoRes.userInfo.country,
                gender: infoRes.userInfo.gender,
                language: infoRes.userInfo.language,
                nickName: encodeURIComponent(infoRes.userInfo.nickName),
                province: infoRes.userInfo.province
              },
              success: function (res) {
                if (res.data.code == 0) {
                  // console.log('login success');
                  console.log("login success", res.data.data.userId, res.data.data.openId)
                  var skey = res.header.Session;
                  wx.setStorageSync('loginFlag', skey);
                  wx.setStorageSync('userId', res.data.data.userId);
                  wx.setStorageSync('openid', res.data.data.openId);
                  wx.hideLoading()
                  if (res.data.data.mobile != '' && res.data.data.userId != '') {
                    wx.switchTab({
                      url: '../home/home',
                      success: function (e) {
                        var page = getCurrentPages().pop();
                        if (page == undefined || page == null) return;
                        page.onLoad();
                        console.log('switchTab')
                      }
                    })
                    wx.showToast({
                      title: '登录成功',
                      icon: 'none',
                      duration: 1500,
                      mask: true
                    });

                  } else {
                    wx.navigateTo({
                      url: '../bindPhone/bindPhone',
                    })
                  }

                } else {
                  wx.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 1500,
                    mask: true
                  });
                  wx.hideLoading();
                }

              },

              fail: function (error) {
                // 调用服务端登录接口失败
                app.showInfo('调用接口失败');
                console.log(error);
              }
            });
          },

          fail: function (error) {
            // 获取 userInfo 失败，去检查是否未开启权限
            console.log(error)
            wx.hideLoading();

            // that.checkUserInfoPermission();
          }
        });

      } else {
        // 获取 code 失败
        app.showInfo('登录失败');
        console.log('调用wx.login获取code失败');
      }
    },

    fail: function (error) {
      // 调用 wx.login 接口失败
      app.showInfo('接口调用失败');
      console.log(error);
    }
  });
}
module.exports = checkLogin;